package com.ylx.web;

import com.ylx.pojo.User;
import com.ylx.service.Autowired;
import com.ylx.service.UserService;
import com.ylx.service.UserServiceIpml;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;


@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    @Autowired
    private UserService userService;

    @Override
    protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/json;charset=utf-8");
        //获取前端的验证码
        String checkcode = req.getParameter("checkcode");
        //判断验证码是否正确
        //逻辑：获取到用户输入的验证码和session中的验证码进行比较
        //获取session中的验证码
        HttpSession session = req.getSession();
        String code = (String) session.getAttribute("code");
        //比较表示不区分大小写比较
        if (!checkcode.equalsIgnoreCase(code)) {
            //保存一个登录信息到session中
            session.setAttribute("loginMsg", "验证码错误");
            //跳转到登录界面
            req.getRequestDispatcher("login.jsp").forward(req, resp);
            return;//如果验证码都没有验证成功则直接结束
        }

        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String role = req.getParameter("role");
        String remember = req.getParameter("remember");
        UserService userService = new UserServiceIpml();

        //经理页面
   if (role.equals("餐厅经理")){
       User user = userService.select(username, password, role);
        if (user != null) {
            if ("1".equals(remember)) {
                Cookie c_username = new Cookie("username", username);
                Cookie c_password = new Cookie("password", password);
                c_username.setMaxAge(60 * 60 * 24 * 7);
                c_password.setMaxAge(60 * 60 * 24 * 7);
                resp.addCookie(c_username);
                resp.addCookie(c_password);
            }
            Cookie[] cookies = req.getCookies();
            String lasttime = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date());
            if (cookies != null) {
                for (Cookie cookie : cookies) {
                    String name = cookie.getName();
                    if (name.equals("lasttime")) {
                        lasttime = URLDecoder.decode(cookie.getValue(), "utf-8");
                    }
                }
            }
            session.setAttribute("lasttimesession", lasttime);
            Cookie cookie = new Cookie("lasttime", URLEncoder.encode(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), "utf-8"));
            resp.addCookie(cookie);
            session.setAttribute("us", user);
            session.setAttribute("type", "1");
            req.getRequestDispatcher("main.jsp").forward(req, resp);
        } else {
            session.setAttribute("errorMsg", "用户名或密码错误");
            //跳转到登录界面
            req.getRequestDispatcher("login.jsp").forward(req, resp);

        }
    }
        System.out.println(role);
        if (role.equals("厨房主管")){
            User user = userService.select(username, password, role);
            if (user != null) {
                if ("1".equals(remember)) {
                    Cookie c_username = new Cookie("username", username);
                    Cookie c_password = new Cookie("password", password);
                    c_username.setMaxAge(60 * 60 * 24 * 7);
                    c_password.setMaxAge(60 * 60 * 24 * 7);
                    resp.addCookie(c_username);
                    resp.addCookie(c_password);
                }
                Cookie[] cookies = req.getCookies();
                String lasttime = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date());
                if (cookies != null) {
                    for (Cookie cookie : cookies) {
                        String name = cookie.getName();
                        if (name.equals("lasttime")) {
                            lasttime = URLDecoder.decode(cookie.getValue(), "utf-8");
                        }
                    }
                }
                session.setAttribute("lasttimesession", lasttime);
                Cookie cookie = new Cookie("lasttime", URLEncoder.encode(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), "utf-8"));
                resp.addCookie(cookie);
                session.setAttribute("us", user);
                session.setAttribute("type", "2");
                req.getRequestDispatcher("main.jsp").forward(req, resp);
            } else {
                session.setAttribute("errorMsg", "用户名或密码错误");
                //跳转到登录界面
                req.getRequestDispatcher("login.jsp").forward(req, resp);

            }
        }
        if (role.equals("财务管理")){
            User user = userService.select(username, password, role);
            if (user != null) {
                if ("1".equals(remember)) {
                    Cookie c_username = new Cookie("username", username);
                    Cookie c_password = new Cookie("password", password);
                    c_username.setMaxAge(60 * 60 * 24 * 7);
                    c_password.setMaxAge(60 * 60 * 24 * 7);
                    resp.addCookie(c_username);
                    resp.addCookie(c_password);
                }
                Cookie[] cookies = req.getCookies();
                String lasttime = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date());
                if (cookies != null) {
                    for (Cookie cookie : cookies) {
                        String name = cookie.getName();
                        if (name.equals("lasttime")) {
                            lasttime = URLDecoder.decode(cookie.getValue(), "utf-8");
                        }
                    }
                }
                session.setAttribute("lasttimesession", lasttime);
                Cookie cookie = new Cookie("lasttime", URLEncoder.encode(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), "utf-8"));
                resp.addCookie(cookie);
                session.setAttribute("us", user);
                session.setAttribute("type", "3");
                req.getRequestDispatcher("main.jsp").forward(req, resp);
            } else {
                session.setAttribute("errorMsg", "用户名或密码错误");
                //跳转到登录界面
                req.getRequestDispatcher("login.jsp").forward(req, resp);

            }
        }
        if (role.equals("配餐员")){
            User user = userService.select(username, password, role);
            if (user != null) {
                if ("1".equals(remember)) {
                    Cookie c_username = new Cookie("username", username);
                    Cookie c_password = new Cookie("password", password);
                    c_username.setMaxAge(60 * 60 * 24 * 7);
                    c_password.setMaxAge(60 * 60 * 24 * 7);
                    resp.addCookie(c_username);
                    resp.addCookie(c_password);
                }
                Cookie[] cookies = req.getCookies();
                String lasttime = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date());
                if (cookies != null) {
                    for (Cookie cookie : cookies) {
                        String name = cookie.getName();
                        if (name.equals("lasttime")) {
                            lasttime = URLDecoder.decode(cookie.getValue(), "utf-8");
                        }
                    }
                }
                session.setAttribute("lasttimesession", lasttime);
                Cookie cookie = new Cookie("lasttime", URLEncoder.encode(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), "utf-8"));
                resp.addCookie(cookie);
                session.setAttribute("us", user);
                session.setAttribute("type", "4");
                req.getRequestDispatcher("main.jsp").forward(req, resp);
            } else {
                session.setAttribute("errorMsg", "用户名或密码错误");
                //跳转到登录界面
                req.getRequestDispatcher("login.jsp").forward(req, resp);

            }
        }

    }

    @Override
    protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
